package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.AskForleave;
import tgc.edu.exam.entity.Student;
import tgc.edu.exam.entity.SysRole;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.entity.Teacher;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.AskForleaveService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.web.form.AskForleaveForm;
@Controller
@RequestMapping(value="/askForleave")
public class AskForleaveController extends CommonController<AskForleave, UUID, AskForleaveForm>{

	@Autowired
	private AskForleaveService service;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private UserUtils userUtils;
	
	
	@RequestMapping(value="/instant")
	@ResponseBody
	public Object instant() {
		String msg = "数据出错！请联系管理员";
		Specification<AskForleave> spec = buildSpec1();
		List<AskForleave> students = service.findAll(spec);
		if(students.size()!=0) {
			for (AskForleave askForleave : students) {
				askForleave.setState(2);
				service.save(askForleave);
			}
			msg="操作完成！";
		}else {
			msg = "没有要审核的数据哟！";
		}
		return msg;
	}

	@RequestMapping(value="/agree")
	@ResponseBody
	public Object agree(String id) {
		String msg = "数据出错！请联系管理员";
		AskForleave askForleave = service.findById(UUID.fromString(id));
		if(askForleave!=null) {
			askForleave.setState(2);
			service.save(askForleave);
			msg="操作成功！";
		}
		return msg;
	}
	@RequestMapping(value="/refuse")
	@ResponseBody
	public Object refuse(String id,String reason) {
		String msg = "数据出错！请联系管理员";
		AskForleave askForleave = service.findById(UUID.fromString(id));
		if(askForleave!=null) {
			askForleave.setState(3);
			askForleave.setReason(reason);
			service.save(askForleave);
			msg="操作成功！";
		}
		return msg;
	}

	private Specification<AskForleave> buildSpec1() {
		Teacher teacher = (Teacher) userUtils.getUser();
		List<TbClass> list = tbClassService.findByTeacherIdUsername(teacher.getUsername());
		Specification<AskForleave> spec=new Specification<AskForleave>() {

			@Override
			public Predicate toPredicate(Root<AskForleave> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				for (TbClass tbClass : list) {
					rules.add(cb.equal(root.get("tbClass"), tbClass.getName()));
				}
				rules.add(cb.equal(root.get("state"), 1));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@Override
	public Specification<AskForleave> buildSpec(AskForleaveForm form) {
		Teacher teacher = (Teacher) userUtils.getUser();
		List<TbClass> list = tbClassService.findByTeacherIdUsername(teacher.getUsername());
		Specification<AskForleave> spec=new Specification<AskForleave>() {

			@Override
			public Predicate toPredicate(Root<AskForleave> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				for (TbClass tbClass : list) {
					rules.add(cb.equal(root.get("tbClass"), tbClass.getName()));
				}
				rules.add(cb.equal(root.get("state"), 1));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	
	
	
	
	
}
